package states
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	import levels.*;
	import states.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class MenuState extends FlxState
	{
		// Selections
		private static const SELECT_LEFT:uint = 0;
		private static const SELECT_CENTER:uint = 1;
		private static const SELECT_RIGHT:uint = 2;
		
		// Background items
		private static var _bg:FlxSprite;
		private static var _overlay:FlxSprite;
		private static var _eyes:FlxSprite;
		private static var _torches:FlxSprite;
		private static var _title:FlxSprite;
		private static var _versionText:FlxText;
		private static var _menuInstructions:FlxText;
		private static var _leftButton:FlxExtendedSprite;
		private static var _centerButton:FlxExtendedSprite;
		private static var _rightButton:FlxExtendedSprite;
		
		// States
		private static var _nextState:Class;
		private static var _selection:uint;
		
		override public function create():void
		{
			super.create();
			FlxG.mouse.show();
			
			addBackground();
			checkProgress();
			
			MusicPlayer.play(MusicPlayer.MENU);
			Registry.paused = false;
			add(Registry.animations);
		}
		
		override public function destroy():void
		{
			clear();
			FlxG.mouse.hide();
			
			super.destroy();
		}
		
		override public function update():void
		{
			super.update();
			
			if (Registry.paused)
			{
				return;
			}
			
			if (Registry.isNewPlayer)
			{
				Animation.finger(new FlxPoint(355, 515), 3);
			}
			
			updateSelections();
			animateSelections();
			checkInputs();
		}
		
		public static function init():void
		{
			_selection = SELECT_CENTER;
			initBackground();
		}
		
		private static function initBackground():void
		{
			_overlay = new FlxSprite();
			_overlay.loadGraphic(Assets.ImgMenuOverlay, false, false, FlxG.width, FlxG.height, true);
			_overlay.alpha = 0.5;
			
			_title = new FlxSprite();
			_title.loadGraphic(Assets.ImgMenuTitle, false, false, FlxG.width, FlxG.height, true);
			
			_versionText = new FlxText(0, 0, FlxG.width);
			_versionText.alignment = "right";
			_versionText.size = 10;
			_versionText.color = 0xFFFFFFFF;
			_versionText.text = Main.VERSION;
			
			_menuInstructions = new FlxText(0, 0, FlxG.width);
			_menuInstructions.font = "cooper_black";
			_menuInstructions.alignment = "center";
			_menuInstructions.size = 20;
			_menuInstructions.color = 0xFFFFFFFF;
			_menuInstructions.text = "For Keyboard: LEFT/RIGHT to select. SPACE to confirm.";
			_menuInstructions.y = FlxG.height - _menuInstructions.height;
			
			_leftButton = new FlxExtendedSprite();
			_leftButton.loadGraphic(Assets.ImgMenuLevels, true, false, 143, 35, true);
			_leftButton.addAnimation("default", [0], 1, false);
			_leftButton.addAnimation("blink", [1, 0], 6);
			_leftButton.x = FlxG.width * 3 / 25;
			_leftButton.y = FlxG.height - 60;
			_leftButton.clickable = true;
			_leftButton.enableMouseClicks(true);
			_leftButton.mouseReleasedCallback = selectionReleasedCallback;
			
			_centerButton = new FlxExtendedSprite();
			_centerButton.addAnimation("default", [0], 1);
			_centerButton.addAnimation("blink", [1, 0], 6);
			_centerButton.y = FlxG.height - 60;
			_centerButton.clickable = true;
			_centerButton.enableMouseClicks(true);
			_centerButton.mouseReleasedCallback = selectionReleasedCallback;
			
			_rightButton = new FlxExtendedSprite();
			_rightButton.loadGraphic(Assets.ImgMenuOptions, true, false, 158, 36, true);
			_rightButton.addAnimation("default", [0], 1);
			_rightButton.addAnimation("blink", [1, 0], 6);
			_rightButton.x = FlxG.width * 35 / 50;
			_rightButton.y = FlxG.height - 60;
			_rightButton.clickable = true;
			_rightButton.enableMouseClicks(true);
			_rightButton.mouseReleasedCallback = selectionReleasedCallback;
		}
		
		private static function selectionReleasedCallback(obj:FlxExtendedSprite, x:int, y:int):void
		{
			if (obj == _leftButton)
			{
				if (Registry.isNewPlayer)
				{
					return;
				}
				switchState(SelectState);
			}
			else if (obj == _centerButton)
			{
				switchState(Registry.progress);
			}
			else if (obj == _rightButton)
			{
				switchState(OptionState);
			}
		}
		
		private function checkProgress():void
		{
			if (Registry.isNewPlayer)
			{
				_centerButton.loadGraphic(Assets.ImgMenuStart, true, false, 121, 37, true);
				_leftButton.alpha = 0.5;
				
				var _spacebar:FlxSprite = new FlxSprite(320, 500);
				_spacebar.loadGraphic(Assets.ImgButtonHorizontal, true, false, 80, 20);
				_spacebar.addAnimation("pressed", [0, 2], 6);
				_spacebar.play("pressed");
				add(_spacebar);
				
				var _spacebarText:FlxText = new FlxText(328, 500, FlxG.width, "S P A C E");
				_spacebarText.size = 13;
				_spacebarText.font = "cooper_black";
				_spacebarText.color = 0xFF000000;
				
				add(_spacebarText);
			}
			else
			{
				_centerButton.loadGraphic(Assets.ImgMenuContinue, true, false, 160, 36, true);
				_leftButton.alpha = 1.0;
			}
			_centerButton.x = FlxG.width / 2 - _centerButton.width / 2;
		}
		
		private function updateSelections():void
		{
			if (FlxG.keys.justPressed("LEFT"))
			{
				if (_selection != SELECT_LEFT)
				{
					_selection--;
					if (Registry.isNewPlayer && _selection == SELECT_LEFT)
					{
						_selection = SELECT_CENTER;
					}
					else
					{
						FlxG.play(Assets.SndClick, Registry.soundVolume);
					}
				}
			}
			
			if (FlxG.keys.justPressed("RIGHT"))
			{
				if (_selection != SELECT_RIGHT)
				{
					_selection++;
					FlxG.play(Assets.SndClick, Registry.soundVolume);
				}
			}
			
			if (_leftButton.mouseOver && _selection != SELECT_LEFT)
			{
				if (!Registry.isNewPlayer)
				{
					_selection = SELECT_LEFT;
					FlxG.play(Assets.SndClick, Registry.soundVolume);
				}
			}
			
			if (_centerButton.mouseOver && _selection != SELECT_CENTER)
			{
				_selection = SELECT_CENTER;
				FlxG.play(Assets.SndClick, Registry.soundVolume);
			}
			
			if (_rightButton.mouseOver && _selection != SELECT_RIGHT)
			{
				_selection = SELECT_RIGHT;
				FlxG.play(Assets.SndClick, Registry.soundVolume);
			}
		}
		
		private function animateSelections():void
		{
			switch (_selection)
			{
				case SELECT_LEFT: 
				{
					_leftButton.play("blink");
					_centerButton.play("default");
					_rightButton.play("default");
					break;
				}
				case SELECT_CENTER: 
				{
					_leftButton.play("default");
					_centerButton.play("blink");
					_rightButton.play("default");
					break;
				}
				case SELECT_RIGHT: 
				{
					_leftButton.play("default");
					_centerButton.play("default");
					_rightButton.play("blink");
					break;
				}
				default: 
				{
					break;
				}
			}
		}
		
		private function checkInputs():void
		{
			//if (FlxG.keys.justPressed("TAB"))
			//{
				//FlxG.switchState(new Tutorial_3());
			//}
			
			if (FlxG.keys.justPressed("SPACE"))
			{
				switch (_selection)
				{
					case SELECT_LEFT: 
					{
						switchState(SelectState);
						break;
					}
					case SELECT_CENTER: 
					{
						switchState(Registry.progress);
						break;
					}
					case SELECT_RIGHT: 
					{
						switchState(OptionState);
						break;
					}
					default: 
					{
						break;
					}
				}
			}
			
			if (FlxG.keys.SHIFT && FlxG.keys.justPressed("DELETE"))
			{
				Registry.clearProgress();
				_selection = SELECT_CENTER;
				switchState(MenuState);
			}
			
			//if (FlxG.keys.SHIFT && FlxG.keys.justPressed("R"))
			//{
				//Registry.unlockAllLevels();
				//_selection = SELECT_CENTER;
				//switchState(MenuState);
			//}
		}
		
		private function addBackground():void
		{
			add(Registry.epicBackground);
			add(_overlay);
			add(_title);
			add(_versionText);
			add(_menuInstructions);
			add(_leftButton);
			add(_centerButton);
			add(_rightButton);
		}
		
		private static function switchState(nextState:Class):void
		{
			_nextState = nextState;
			Registry.paused = true;
			FlxG.play(Assets.SndTeleport, Registry.soundVolume);
			FlxG.fade(0xffaaaaaa, 1.5, onFade);
		}
		
		private static function onFade():void
		{
			Registry.paused = false;
			FlxG.switchState(new _nextState());
		}
	}
}